using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class HerbScrollRECT : MonoBehaviour,IBeginDragHandler,IEndDragHandler
{
    private GridLayoutGroup _grid;//网格布局
    
    //拖动名
    private ScrollRect _scroll;
    public bool _openfirst = true;

    private GameObject _finHerbGrid;
    public  float HerbGridchildCount;//FINHerbGrid的子物体
    public float[] pagePosition;//子物体数组间隔
    private int _page = 1;
    
    // Start is called before the first frame update
    void Start()
    {
        _scroll = GetComponent<ScrollRect>();
        _finHerbGrid = GameObject.FindWithTag("HerbGrid");
        HerbContorl.Instance.PageNumber = 1;
    }

    // Update is called once per frame
    void Update()
    {
        
         SetGridWidth();
         if (_openfirst)
         {
             HerbContorl.Instance.PageNumber = 1;
             _scroll.horizontalNormalizedPosition = pagePosition[0];
             _openfirst = false;
         }

         HerbContorl.Instance.PageNumber = _page;

    }
    
    //自适应宽度
    public void SetGridWidth()
    {
        _grid = _finHerbGrid.gameObject.GetComponent<GridLayoutGroup>();
        HerbGridchildCount = _finHerbGrid.gameObject.transform.childCount;//获取子物体个数
        float width = (Mathf.Ceil(HerbGridchildCount / 2)) * 700;
        _grid.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,width);
        
        //获取子物体数组间隔
        float pageX = 1 / (Mathf.Ceil(HerbGridchildCount / 2) - 1);
        pagePosition = new float[(int)Mathf.Ceil(HerbGridchildCount / 2)];
        for (int i = 0; i < (int)Mathf.Ceil(HerbGridchildCount / 2); i++)
        {
            if (i == 0)
            {
                pagePosition[i] = 0;
            }else if (i == (int)Mathf.Ceil(HerbGridchildCount / 2)-1)
            {
                pagePosition[i] = 1;
            }
            else
            {
                pagePosition[i] = pagePosition[i-1] + pageX;
            }
        }
    }
    
    //点击事件
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("开始拖拽");
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        float currentPosition = _scroll.horizontalNormalizedPosition;//当前位置
        int index = 0;
        float offset = currentPosition - pagePosition[0];//偏移量
        
        for (int i = 1; i < pagePosition.Length; i++)
        {
            if (Mathf.Abs(currentPosition - pagePosition[i]) < offset)
            {
                index = i;
                offset = Mathf.Abs(currentPosition - pagePosition[i]);
                if (HerbContorl.Instance.PageNumber < pagePosition.Length)
                {
                  //  HerbContorl.Instance.PageNumber++;
                    _page++;
                }
            }
            else
            {
                if (HerbContorl.Instance.PageNumber >1)
                {
                 //   HerbContorl.Instance.PageNumber--;
                    _page--;
                }
            }
        }
        _scroll.horizontalNormalizedPosition = pagePosition[index];
     }
    
    
}
